#ifndef ACUASSOFTUART_H
#define ACUASSOFTUART_H
/**
 @defgroup ACUAS_EPsoftuart ACUAS Easy Programming softUART
 @brief C macros to easily program UART of the ACUAS ATmega16/ATmega32 board.
 @author Martin Ossmann, FH Aachen
*/

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "SOFTUART\softUART.h"

#ifndef SOFTUART_BAUD_RATE
/// default baud rate
#define SOFTUART_BAUD_RATE 1200
#endif

/*@{*/
/// @file ACUASsoftuart.h
/// @file ACUASsoftuart.c
/** \remarks
* Wiring of the softUART connector to the controller:
\code
*  Connector              controller
* RxD (receive data)  -> port B, pin2
* TxD (transmit data) -> port B, pin3
\endcode
*/
/// Global Variable used by SOFTUART_NUMBER to store the alphanumeric representation of the number
unsigned char softuart_str[21];

void softuart_read_line(unsigned char * c_array, uint8_t max, int try_sec);

/// Initialises the soft UART. Must be invoked in the PROGRAM_INIT part. Sets frame format: asynchronous, 8 data bits, no parity, 1 stop bit
#define INIT_SOFTUART {Timer0init(); TX1init(); RX1init(); sei();}
/// Sends a string which is located in SRAM over soft UART
#define SOFTUART_TEXT(string) TX1string((char*) string);
/// Sends a string which is located in flash ROM over soft UART. The string must be declared as: \code static const char PText[] PROGMEM = "text" \endcode
#define SOFTUART_TEXT_CONST(string) TX1string_p(string);
/// Sends a number over soft UART
#define SOFTUART_NUMBER(x) {itoa( x, (char*)softuart_str, 10); TX1string((char*)softuart_str);}
/// Sends one character over soft UART
#define SOFTUART_CHAR(c) TX1char(c);
/// Sends a linefeed over soft UART
#define SOFTUART_CRLF TX1char(13); TX1char(10);
/// Reads one character from soft UART
#define SOFTUART_READ_CHAR(c) c = getRX1char_nw();
///
/**
 *  @brief   Reads one line of text delimeted by a carriage return (ASCII 13). The function waits until a carriage return is received or max_char characters have been received
 *  @param   c_array a char-array which can contain at least max_char characters + 1
 *  @param   max_char the maximum amount of characters which are received if no carriage return is received.
 *  @return  none
 */
#define SOFTUART_READ_LINE(c_array,max_char) softuart_read_line((c_array), (max_char), 5);

/*@}*/

#endif // ACUASSOFTUART_H
